import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:xianyu_school/utils/url.dart';
import 'package:xianyu_school/xianyuApp/entity/userData/data.dart';
import 'package:image_picker/image_picker.dart';
import 'package:xianyu_school/xianyuApp/mine/setintroduction.dart';
import 'package:xianyu_school/xianyuApp/mine/setname.dart';

import 'Widgets/BallPulseIndicator.dart';

class settingPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _settingPageState();
  }
}

class _settingPageState extends State<settingPage>{

  var url = urls().url;
  data data1 = new data();
//
  Future gethttp() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    data1.username = prefs.getString("username");
    try{
      Response response = await Dio().get(url + "/users/data",queryParameters:{"username":data1.username});
      return response;
    }
    catch(e){
      return ;
    }
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void deactivate() {
    super.deactivate();
    var bool = ModalRoute.of(context).isCurrent;
    if (bool) {
      initState(); //需要调用的方法
    }
  }

  @override
  Widget build(BuildContext context) {

    return FutureBuilder(
        future: gethttp(),
        builder: (context, snapshot){
          if(snapshot.hasData) {
            Map resultmap = json.decode(snapshot.data.toString());
            data1 = data.fromJson(resultmap);
            return Scaffold(
                appBar: CupertinoNavigationBar(
                  backgroundColor: Colors.yellow,
                  middle: const Text('个人资料'),
                ),
                body: ListView(
                  children: [
                    Container(
                      height: 40,
                      alignment: Alignment(-1,1),
                      child: Text(
                        "  基本信息",
                        style: TextStyle(
                            fontSize: 15.0
                        ),
                      ),
                    ),
                    Container(
                      padding: EdgeInsets.all(10.0),
                      height: 350.0,
                      decoration: BoxDecoration(
                        border: Border.all(width: 2.0, color: Color.fromRGBO(220,220,220, 1.0)),
                        color:Color.fromRGBO(220,220,220, 1.0),
                        borderRadius: BorderRadius.all(Radius.circular(20.0)),
                      ),
                      child: ListView(
                        children: [
                          Row(
                            children: [
                              Text("头像"),
                              Expanded(
                                child: Text(" "),
                              ),
                              Container(
                                child: Image.memory(
                                  base64.decode(data1.img),
                                  fit: BoxFit.fill,
                                  gaplessPlayback:true,
                                  height: 70,
                                  width: 70,
                                ),
                              ),
                              IconButton(
                                icon: Icon(Icons.chevron_right),
                                iconSize: 30,
                                color: Colors.grey,
                                onPressed: () async {
                                  var image = await ImagePicker.pickImage(source: ImageSource.gallery);
                                  List<int> test = image.readAsBytesSync();
                                  String img = base64Encode(test);
                                  Response re = await Dio().post(url+"/users/dataUp",data:"{\"img\":" + "\""+img+"\"" + ",\"username\""+":"+"\""+data1.username+"\""+"}");
                                  setState(() {
                                    data1.img = img;
                                  });
                                },
                              )
                            ],
                          ),
                          Row(
                            children: [
                              Text("昵称"),
                              Expanded(
                                child: Text(" "),
                              ),
                              Text(data1.nickname,
                                  style: TextStyle(fontSize: 16)),
                              IconButton(
                                icon: Icon(Icons.chevron_right),
                                iconSize: 30,
                                color: Colors.grey,
                                onPressed: () {
                                  Navigator.push<String>(context, MaterialPageRoute(builder: (_) {
                                    return setname();
                                  })).then((String value) async {
                                    if(value.isEmpty) return;
                                    else {
                                      Response re = await Dio().post(url+"/users/dataUp",data:"{\"nickname\":" + "\""+value+"\"" + ",\"username\""+":"+"\""+data1.username+"\""+"}");
                                      setState(() {
                                        data1.nickname = value;
                                      });
                                    }
                                  });
                                },
                              )
                            ],
                          ),
                          Row(
                            children: [
                              Text("性别"),
                              Expanded(
                                child: Text(" "),
                              ),
                              Text(data1.sex,
                                  style: TextStyle(fontSize: 16)),
                              IconButton(
                                icon: Icon(Icons.chevron_right),
                                iconSize: 30,
                                color: Colors.grey,
                                onPressed: () async {
                                  var alertDialogs = await showDialog(
                                      context: context,
                                      builder: (context) {
                                        return SimpleDialog(
                                          children: [
                                            FlatButton(
                                                child: Text("男"),
                                                onPressed: () async {
                                                  Response re = await Dio().post(url+"/users/dataUp",data:"{\"sex\":" + "\""+"男"+"\"" + ",\"username\""+":"+"\""+data1.username+"\""+"}");
                                                  setState(() {
                                                    data1.sex = "男";
                                                  });
                                                  Navigator.pop(context);
                                                }
                                            ),
                                            FlatButton(
                                                child: Text("女"),
                                                onPressed: () async {
                                                  Response re = await Dio().post(url+"/users/dataUp",data:"{\"sex\":" + "\""+"女"+"\"" + ",\"username\""+":"+"\""+data1.username+"\""+"}");
                                                  setState(() {
                                                    data1.sex = "女";
                                                  });
                                                  Navigator.pop(context);
                                                }
                                            )
                                          ],
                                        );
                                      });
                                  return alertDialogs;
                                },
                              )
                            ],
                          ),
                          Row(
                            children: [
                              Text("年龄"),
                              Expanded(
                                child: Text(" "),
                              ),
                              Text(data1.age.toString(),
                                  style: TextStyle(fontSize: 16)),
                              IconButton(
                                icon: Icon(Icons.chevron_right),
                                iconSize: 30,
                                color: Colors.grey,
                                onPressed: () {

                                },
                              )
                            ],
                          ),
                          Row(
                            children: [
                              Text("简介"),
                              Expanded(
                                child: Text(" "),
                              ),
                              IconButton(
                                icon: Icon(Icons.chevron_right),
                                iconSize: 30,
                                color: Colors.grey,
                                onPressed: () {
                                  Navigator.push<String>(context, MaterialPageRoute(builder: (_) {
                                    return setintroduction();
                                  })).then((String value) async {
                                    if(value.isEmpty) return;
                                    else{
                                      Response re = await Dio().post(url+"/users/dataUp",data:"{\"introduction\":" + "\""+value+"\"" + ",\"username\""+":"+"\""+data1.username+"\""+"}");
                                      setState(() {
                                        data1.introduction = value;
                                      });
                                    }
                                  });
                                },
                              )
                            ],
                          ),
                          Text(data1.introduction)
                        ],
                      ),
                    ),
                    Row(
                      children: [
                        Expanded(child: Text(" ")),
                        OutlineButton(
                            child: Text("提交"),
                            textColor: Colors.orange,
                            color: Colors.white,
                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                            borderSide: BorderSide(color: Colors.orange, width: 1),
                            onPressed:() {
                              Navigator.pop(context);
                            })
                      ],
                    ),
                    Row(
                      children: [
                        Expanded(child: Text(" ")),
                        OutlineButton(
                            child: Text("退出登录"),
                            textColor: Colors.orange,
                            color: Colors.red,
                            shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                            borderSide: BorderSide(color: Colors.orange, width: 1),
                            onPressed:() async {
                              SharedPreferences pref = await SharedPreferences.getInstance();
                              Navigator.pop(context);
                              pref.clear();
                            })
                      ],
                    )
                  ],
                )
            );
          }
          else return BallPulseIndicator(
              color: Colors.white
          );
        }
    );
  }
}